home *** CD-ROM | disk | FTP | other *** search
/ Total Java Scripts / Total Java Scripts.iso / JavaApplets / messages / banner / Banner3.java < prev    next >
Encoding:
Java Source  |  1998-10-31  |  7.2 KB  |  291 lines

  1. import java.awt.*;
  2. import java.net.*;
  3.  
  4.  
  5. public class Banner3 extends java.applet.Applet implements Runnable 
  6. {    
  7.  
  8. /****************************/
  9. /*public integer variables  */
  10. /****************************/
  11.     public int width; 
  12.     public int height;
  13.     public int y = 0; //current position y
  14.     public int j = 0; //counter
  15.     public int i = 0; //counter
  16.     
  17.     public int fheight = 0; //font height
  18.     public int xstart = 0;  //position at which x starts
  19.     public int ystart = 0;  //position at which y starts
  20.     public int xstop = 0;   //position at which x stops
  21.     public int ystop = 0;   //position at which y stops
  22.     public int msgwidth;    //width of msg window
  23.     
  24. /***********************/
  25. /*public integer arrays*/
  26. /***********************/
  27.  
  28.     public int[] catfwidth; //integer array of font widths for the category strings
  29.     public int[] msgfwidth; //integer array of font widths for the message strings
  30.     public int[] msgx;      //integer array of the center of message
  31.     public int[] catx;      //integer array of the center of category
  32.     
  33.     boolean slept = false;
  34.     // Other control variables
  35.     
  36. /*************************/
  37. /*static object variables*/
  38. /*************************/
  39.     static FontMetrics metrics;
  40.     static Graphics gc = null;
  41.     static Graphics g;
  42.     static Image offscreen;
  43.     static Rectangle apprect;
  44.     
  45.  
  46. /********************************************/
  47. /*static variables parsed in ParseArgs class*/
  48. /********************************************/
  49.     static String fontname;
  50.     static int fontsize;
  51.     static int fontstyle;
  52.     static Font font;
  53.    
  54.    static Color msgbgcolor;        //message area backgroud color
  55.    static Color[] catbgcolor;        //category area background color
  56.    static Color tempColor;    
  57.     static Color linkColor;        //color of msgtext when mouse enter
  58.     static Color msgtextcolor;    //message text color
  59.    static Color[] cattextcolor;      //category text color
  60.     
  61.     static int totalmessages;    //total number of messages
  62.     static String[] msgtext;    //array of message strings
  63.     static String[] cattext;    //array of category strings
  64.     static String[] textURL;    //array of URL strings
  65.    
  66.     static String valign;
  67.     static String align;
  68.     static String direction;
  69.    
  70.     static int delay;            //what does this do?
  71.     int speed;            //what does this do?
  72.     static int catwidth;        //width of the category space
  73.     static int pauseDelay;        //amount of time of delay(milliseconds)
  74.     static int xdelta;            //change in x
  75.     static int ydelta;            //change in y
  76.  
  77.     private Thread juicer;        //our thread
  78.  
  79.  
  80. /*************/
  81. /*init method*/
  82. /*************/
  83.     public void init() 
  84.         {       ParseArgs3 pa = new ParseArgs3(this);
  85.  
  86.         g = getGraphics();
  87.           
  88.       tempColor = msgtextcolor;
  89.         
  90.         //initialize the arrays
  91.         catfwidth = new int [totalmessages];
  92.         catx = new int [totalmessages];
  93.         msgfwidth = new int [totalmessages];
  94.         msgx = new int[totalmessages];
  95.         msgfwidth = new int[totalmessages];
  96.  
  97.         //get the width and height of the applet
  98.         apprect = bounds();
  99.         width = apprect.width;
  100.         height = apprect.height;
  101.         resize(width, height);
  102.  
  103.         offscreen = createImage(width, height);
  104.         
  105.         //instantiate the font object
  106.         font = new Font(fontname, fontstyle, fontsize);
  107.  
  108.         metrics = g.getFontMetrics(font);
  109.         fheight = metrics.getMaxAscent() - metrics.getMaxDescent() + 2;
  110.       
  111.         msgwidth = width - catwidth;
  112.       
  113.         for( i=0; i < totalmessages; i++) 
  114.         {    catfwidth[i] = metrics.stringWidth(cattext[i]);
  115.             catx[i] = ( (catwidth - catfwidth[i]) / 2);
  116.             
  117.             msgfwidth[i] = metrics.stringWidth(msgtext[i]);
  118.             msgx[i] = ( (msgwidth - msgfwidth[i]) / 2 + catwidth);
  119.         }
  120.         i=0;
  121.  
  122.         try 
  123.         {    if(direction.equalsIgnoreCase("BottomTop")) 
  124.             {    ystart = y = height + fheight;
  125.                 ystop = -fheight;
  126.                 int dist = Math.abs(ystop - ystart);
  127.                 if(dist % ydelta != 0)
  128.                 ystop -= ydelta - (dist % ydelta);
  129.                 xstart = msgx[0];
  130.                 xstop = xdelta = 0;
  131.                 ydelta = -ydelta;
  132.             }
  133.             
  134.             else if(direction.equalsIgnoreCase("TopBottom")) 
  135.             {    ystart = y = -fheight;
  136.                 ystop = height + fheight;
  137.                 int dist = Math.abs(ystop - ystart);
  138.                 if(dist % ydelta != 0) 
  139.                     ystop += ydelta - (dist % ydelta);
  140.                 xstart = msgx[0];
  141.                 xstop = xdelta = 0;
  142.             }
  143.         }
  144.         catch (Exception e) 
  145.         {    ystart = y = -fheight;
  146.                 ystop = height + fheight;
  147.                 int dist = Math.abs(ystop - ystart);
  148.                 if(dist % ydelta != 0) 
  149.                     ystop += ydelta - (dist % ydelta);
  150.                 xstart = msgx[0];
  151.                 xstop = xdelta = 0;
  152.         }
  153.     }
  154.  
  155. /**************/
  156. /*start method*/
  157. /**************/
  158.     public void start() 
  159.     {    if(juicer == null) 
  160.         {    juicer = new Thread(this);
  161.             juicer.start();
  162.         }
  163.     }
  164.    
  165. /*************/
  166. /*stop method*/
  167. /*************/
  168.     public void stop() 
  169.     {    if ( (juicer != null) && (juicer.isAlive()) ) 
  170.         {    juicer.stop();
  171.             juicer = null;
  172.         }
  173.     }
  174.  
  175. /************/
  176. /*run method*/
  177. /************/
  178.     public void run() 
  179.     {
  180.         while (juicer != null) 
  181.         {    update(g);
  182.             try
  183.             {    juicer.sleep(1); 
  184.             } 
  185.             catch (InterruptedException e) 
  186.             {    System.out.println("something wrong with juicer");
  187.             }
  188.  
  189.             //test for middle
  190.             if ((y == (height - fheight) / 2 + fheight - 1)
  191.                 && slept == false) 
  192.             {    try 
  193.                 {    juicer.sleep(pauseDelay);
  194.                 }    
  195.                 catch (InterruptedException e)
  196.                 {    System.out.println("juicer pauseDelay");
  197.                 } 
  198.                 slept = true;
  199.             }
  200.                 
  201.             // haven't reached the top
  202.             if(y != ystop) y += ydelta; 
  203.             
  204.             // have reached the top
  205.             else 
  206.             {    y = ystart;  // reset y
  207.                 
  208.             // go back to first message if necessary
  209.                 if (i == (totalmessages - 1) ) 
  210.                 {    i = 0; 
  211.                     slept = false; 
  212.                 }
  213.                 
  214.             // go to next message
  215.                 else
  216.                 {    i++;
  217.                 slept = false;
  218.                 } 
  219.              }
  220.           }
  221.        }
  222.        
  223.        
  224. /*****************/
  225. /*update method  */
  226. /*****************/
  227.     public void update(Graphics g)
  228.     {    paint(g); 
  229.     }
  230.  
  231. /**************/
  232. /*paint method*/
  233. /**************/
  234.     public void paint(Graphics g) 
  235.     {    gc = offscreen.getGraphics();
  236.     
  237.         gc.setFont(font);
  238.         
  239.         //draw and fill the category rectangle
  240.                 gc.setColor(catbgcolor[i]); 
  241.         gc.fillRect(2, 2, catwidth - 4, height- 4);
  242.         
  243.         //draw the category string
  244.                 gc.setColor(cattextcolor[i]); 
  245.         gc.drawString(cattext[i], catx[i], y);
  246.      
  247.         //draw and fill the message rectangle
  248.         gc.setColor(msgbgcolor);
  249.         gc.fillRect(catwidth - 2, 2, width-4, height-4);
  250.         
  251.         //draw the message text centered at msgx
  252.       gc.setColor(tempColor);
  253.         gc.drawString(msgtext[i], msgx[i], y);
  254.         
  255.         //paint the black rectangle
  256.         gc.setColor(Color.black); 
  257.         gc.drawRect(0,0,width -1, height - 1);
  258.         
  259.         //paint the white rectangle
  260.         gc.setColor(Color.white); 
  261.         gc.drawRect(1,1,width -3, height -3);
  262.         //gc.drawRect(2,2, width -5, height -5);
  263.         
  264.         g.drawImage(offscreen, 0, 0, null);
  265.         gc.dispose();
  266.     }
  267.  
  268.     public boolean mouseUp( Event evt, int x , int y ) 
  269.     {    try {    URL thingee = new URL(textURL[i]);
  270.                 getAppletContext().showDocument(thingee);
  271.             }
  272.         catch (MalformedURLException e)
  273.         {    System.out.println("hey I caught it!!!"); 
  274.         }
  275.         return true;
  276.     }
  277.  
  278.     public boolean mouseEnter (Event evt, int x, int y) 
  279.    { 
  280.       tempColor = linkColor;
  281.         update(g);
  282.         return true;
  283.     }
  284.  
  285.     public boolean mouseExit (Event evt, int x, int y) 
  286.    {  tempColor =  msgtextcolor;
  287.         update(g);
  288.         return true;
  289.     }
  290. }
  291.